/*
 * EditProjectController.java
 *
 * Project: TECHWATCH - TESTING TECHNOLOGIES
 *
 * Copyright 2008 by ELCA Informatique SA
 * Av. de la Harpe 22-24, 1000 Lausanne 13
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of ELCA Informatique SA. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license
 * agreement you entered into with ELCA.
 */

package ch.elca.training.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.mvc.CancellableFormController;

import ch.elca.training.dom.Project;
import ch.elca.training.model.UrlConstants;
import ch.elca.training.service.ProjectService;

/**
 * The controller show project's detail.
 *
 * <script type="text/javascript">printFileStatus
 *   ("$URL: https://cvs.elca.ch/subversion/cxnet-portal/trunk/etc/eclipse/preferences_2008_03_03.epf $",
 *    "$Revision: 334 $",
 *    "$Date: 2008-03-03 16:07:32 +0700 (Mon, 03 Mar 2008) $",
 *    "$Author: qkp@ELCA.CH $"
 *    "$Id:$"
 * );</script>
 *
 * @author ltd
 */
public class EditProjectController extends CancellableFormController {
    @SuppressWarnings("unused")
	private static final Log s_logger = LogFactory.getLog(EditProjectController.class); 
    private ProjectService m_projectService;
    
    /**
     * @return the projectService
     */
    public ProjectService getProjectService() {
        return m_projectService;
    }

    /**
     * @param projectService the projectService to set
     */
    public void setProjectService(ProjectService projectService) {
        m_projectService = projectService;
    }   
    
    /**
     * Default constructor.
     */
    public EditProjectController() {
        super();

        setSessionForm(true);
        setCommandName("project");
        setCommandClass(Project.class);
        setFormView(UrlConstants.EDIT_URL);
        setSuccessView(UrlConstants.REDIRECT_PREFIX + UrlConstants.SEARCH_URL);
        setCancelView(UrlConstants.REDIRECT_PREFIX + UrlConstants.SEARCH_URL);
    }

     /**
     * {@inheritDoc}
     */
    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
            throws Exception {
        super.initBinder(request, binder);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(
        		"dd/MM/yyyy"), false));
    }
    
    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
	@Override
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors)
            throws Exception {
        // load the current project
        long pid = Long.parseLong(request.getParameter("pid"));
        Project project = getProjectService().findById(pid);
        if (project == null) {
            project = new Project();
        }
        
        Project inFormProject = (Project) command;
        //inFormProject.copy(project);

        return super.referenceData(request, command, errors);
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void doSubmitAction(Object command) throws Exception {
        Project inFormProject = (Project) command;
        
        getProjectService().saveProject(inFormProject);
        
        super.doSubmitAction(command);
    }
}